package com.filex.ads.common;

import android.graphics.Point;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.filex.ads.network.Networking;



public abstract class BaseUrlGenerator {

    private static final String ADFORMAT_KEY = "adformat";

    /**
     * Bundle ID, as in package name.
     */
    protected static final String BUNDLE_ID_KEY = "bundle";

    private static final String WIDTH_KEY = "w";
    private static final String HEIGHT_KEY = "h";

    /**
     * 1 Mobile/Tablet
     * 2 Personal Computer 3 Connected TV
     * 4 Phone
     * 5 Tablet
     * 6 Connected Device 7 Set Top Box
     */
    private static final String DEVICE_TYPE_KEY = "devicetype";

    private static final String USERAGENT_KEY = "ua";

    private static final String MAKE_KEY = "make";

    private static final String MODEL_KEY = "model";


    private static final String OS_KEY = "os";

    private static final String OS_VERSION_KEY = "osv";

    private static final String CARRIER_KEY = "carrier";

    private static final String CONNECTION_TYPE_KEY = "connectiontype";

    private static final String GID_KEY = "gid";

    protected static final String SDK_VERSION_KEY = "sdk_v";

    protected static final String TEST_KEY = "test";

    private StringBuilder mStringBuilder;
    private boolean mFirstParam;

    public abstract String generateUrlString(String placementId);

    protected void initUrlString(String placementId) {
        mStringBuilder = new StringBuilder(Networking.getScheme()).append("://")
                .append(Constants.HOST).append(Constants.AD_HANDLER).append(placementId);
        mFirstParam = true;
    }

    protected String getFinalUrlString() {
        return mStringBuilder.toString();
    }

    protected void addParam(String key, String value) {
        if (TextUtils.isEmpty(value)) {
            return;
        }
        mStringBuilder.append(getParamDelimiter());
        mStringBuilder.append(key);
        mStringBuilder.append("=");
        mStringBuilder.append(value);
    }

    protected void addParam(String key, Boolean value) {
        if (value == null) {
            return;
        }
        mStringBuilder.append(getParamDelimiter());
        mStringBuilder.append(key);
        mStringBuilder.append("=");
        mStringBuilder.append(value ? "1" : "0");
    }

    private String getParamDelimiter() {
        if (mFirstParam) {
            mFirstParam = false;
            return "?";
        }
        return "&";
    }

    protected void setAppVersion(String appVersion) {
        addParam("av", appVersion);
    }

    protected void setExternalStoragePermission(boolean isExternalStoragePermissionGranted) {
        addParam("android_perms_ext_storage", isExternalStoragePermissionGranted ? "1" : "0");
    }

    protected void setDeviceManufacturer(String make) {
        addParam(MAKE_KEY, make);
    }

    protected void setModel(String model) {
        addParam(MODEL_KEY, model);
    }

    protected void setOS() {
        addParam(OS_KEY, "Android");
    }

    protected void setOSVersion(String osVer) {
        addParam(OS_VERSION_KEY, osVer);
    }



    protected void setCarrierName(String carrier) {
        addParam(CARRIER_KEY, carrier);
    }

    protected void setNetworkType(String nt) {
        addParam(CONNECTION_TYPE_KEY, nt);
    }

    protected void setGAID(String gid) {
        addParam(GID_KEY, gid);
    }

    /**
     * Adds the width and height.
     *
     * @param dimensions The width and height of the screen
     */
    protected void setDeviceDimensions(@NonNull final Point dimensions) {
        addParam(WIDTH_KEY, "" + dimensions.x);
        addParam(HEIGHT_KEY, "" + dimensions.y);
    }

    protected void setSdkVersion(String sdkVersion) {
        addParam(SDK_VERSION_KEY, sdkVersion);
    }


    protected void setBundleId(String bundleId) {
        if (!TextUtils.isEmpty(bundleId)) {
            addParam(BUNDLE_ID_KEY, bundleId);
        }
    }

    protected void setUserAgent(String userAgent) {
        addParam(USERAGENT_KEY, userAgent);
    }

    protected void setDeviceType() {
        addParam(DEVICE_TYPE_KEY, "1");
    }

    protected void setAdFormat(String adFormat) {
        addParam(ADFORMAT_KEY, adFormat);

    }

    protected void setTestMode(boolean testMode) {
        addParam(TEST_KEY, testMode);
    }
}
